﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace eBirdAttend
{
    public partial class DeviceConfigureUserControl : UserControl
    {   //BMJO
        //Do not use it like this. Why you are creating an instance of device configure???
        //If you are expecting that it will return current device that is not going to happen
        //becuse this is a new instance.
        //DeviceConfigure device = new DeviceConfigure();
        //DeviceConfigure device = new DeviceConfigure();  
       
        string _deviceName;
        public DeviceConfigureUserControl()
        {
            InitializeComponent();
        }
        #region Properties

        private string _enabled = "";

        public string Enabled1
        {
            get { return _enabled; }
            set { _enabled = value; }
        }

        private string _logToTextfile;

        public string LogToTextfile
        {
            get { return _logToTextfile; }
            set { _logToTextfile = value; }
        }
        private string _deletelog;

        public string Deletelog
        {
            get { return _deletelog; }
            set { _deletelog = value; }
        }
        private string _InOutStatus = "";

        public string InOutStatus
        {
            get { return _InOutStatus; }
            set { _InOutStatus = value; }
        }

        public static string _id;

        public static string Id
        {
            get { return _id; }
            set { _id = value; }
        }
       
        #endregion Properties

        
        private void DeviceConfigureUserControl_Load(object sender, EventArgs e)
        {
            //MessageBox.Show(_id.ToString());            
            if (_id == null)
                _id = "1";
            //_deviceName = "BioFinger Mini";
            //textBox1.Text = _deviceName + " Configuration";

            DisplayIfExists();  //If this device is already configured,then display the nodes
        }

        private void DisplayIfExists()
        {
            try
            {
                XmlDocument _xmlDocument = new XmlDocument();
                _xmlDocument.Load("config.xml");
                XmlNodeList _nodeList = _xmlDocument.SelectNodes("/eOffice/InstalledDevices/device");
                foreach (XmlNode _xmlNode in _nodeList)
                {
                    if (_xmlNode.Attributes["ID"].Value.ToString() == _id.ToString())
                    {
                        _deviceName = _xmlNode.Attributes["Name"].Value.ToString();
                        textBox1.Text = _deviceName + " Configuration";
                        if (_xmlNode.HasChildNodes)
                        {
                            string _deviceIP = _xmlNode["DeviceIP"].InnerText.ToString();
                            textBox2.Text = _deviceIP;
                            string _devicePort = _xmlNode["DevicePort"].InnerText.ToString();
                            textBox3.Text = _devicePort;
                            string _terminalId = _xmlNode["TerminalId"].InnerText.ToString();
                            textBox4.Text = _terminalId;
                            _InOutStatus = _xmlNode["InOut"].InnerText;
                            if (_InOutStatus == "In")
                                radioButton1.Checked = true;
                            if (_InOutStatus == "Out")
                                radioButton2.Checked = true;
                            if (_InOutStatus == "InOut")
                            {
                                radioButton5.Checked = true;
                                checkBox1.Checked = false;
                            }
                            if (_InOutStatus == "InOutF1F2")
                            {
                                radioButton5.Checked = true;
                                checkBox1.Checked = true;
                            }
                            _logToTextfile = _xmlNode["LogToFile"].InnerText;
                            if (_logToTextfile == "Yes")
                                rbLTYes.Checked = true;
                            else
                                rbLTNo.Checked = true;

                            _deletelog = _xmlNode["DeleteLog"].InnerText;
                            if (_deletelog == "Yes")
                                rbDLYes.Checked = true;
                            else
                                rbDLNo.Checked = true;

                            _enabled = _xmlNode.Attributes["Enabled"].Value.ToString();
                            if (_enabled == "Yes")
                                radioButton3.Checked = true;
                            else
                                radioButton4.Checked = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            if (radioButton3.Checked == true)
                _enabled = "Yes";
            if (radioButton4.Checked == true)
                _enabled = "No";

            if (radioButton1.Checked == true)
                _InOutStatus = "In";
            if (radioButton2.Checked == true)
                _InOutStatus = "Out";
            if (radioButton5.Checked == true)
            {
                if (checkBox1.Checked == false)
                    _InOutStatus = "InOut";
                else
                    _InOutStatus = "InOutF1F2"; 
            }
            if (rbLTYes.Checked == true)
                _logToTextfile = "Yes";
            if (rbLTNo.Checked == true)
                _logToTextfile = "No";
            if (rbDLYes.Checked == true)
                _deletelog = "Yes";
            if (rbDLNo.Checked == true)
                _deletelog = "No";

            SavetoXml();   //Save the device settings to the tag installeddevice
        }

        private void SavetoXml()
        {
            try
            {
                XmlDocument _xmlDocument = new XmlDocument();
                _xmlDocument.Load("config.xml");
                XmlNodeList _nodeList = _xmlDocument.SelectNodes("/eOffice/InstalledDevices/device");

                foreach (XmlNode _xmlNode in _nodeList)
                {
                    if (_xmlNode.Attributes["ID"].Value.ToString() == _id.ToString())
                    {
                        _xmlNode.InnerXml = "<DriverPath></DriverPath><DeviceIP></DeviceIP><DevicePort></DevicePort><TerminalId></TerminalId><InOut></InOut><LogToFile></LogToFile><DeleteLog></DeleteLog>";
                        _xmlNode["DriverPath"].InnerText = "eGuradDev.dll";
                        _xmlNode["DeviceIP"].InnerText = textBox2.Text;
                        _xmlNode["DevicePort"].InnerText = textBox3.Text;
                        _xmlNode["TerminalId"].InnerText = textBox4.Text;
                        _xmlNode["InOut"].InnerText = _InOutStatus;
                        _xmlNode["LogToFile"].InnerText = _logToTextfile;
                        _xmlNode["DeleteLog"].InnerText = _deletelog;
                        _xmlNode.Attributes["Enabled"].Value = _enabled; 

                    }
                }               

                _xmlDocument.Save("config.xml");
                MessageBox.Show("Saved Successfully");
                this.Dispose();
            }
            catch (Exception ex)
            {
            }
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            checkBox1.Enabled = true;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            checkBox1.Enabled = false;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            checkBox1.Enabled = false;
        }
    }
}
